package arrays_and_matrices;

import java.util.Arrays;

/**
 * 题目：改变数组的纬度
 * 例：[[1, 2], [3, 4], [5, 6]] -> [[1, 2, 3], [4, 5, 6]] (3, 2 -> 2, 3)
 *
 * 思路：第一步判断乘积是否相等，嵌套循环 column row, 相当于循环原数组 m, n，
 * 添加 index = 0 作为中间变量[index / n] 的范围是 0 ~ m-1 。 [index % n] 的范围是 0 ~ n-1
 */

public class ReshapeMatrix {

    public static void main(String[] args) {
        ReshapeMatrix reshapeMatrix = new ReshapeMatrix();
        int[][] nums = {{1, 2}, {3, 4}, {5, 6}};
        System.out.println(Arrays.deepToString(reshapeMatrix.reshapeMatrix(nums, 2, 3)));
    }

    public int[][] reshapeMatrix(int[][] matrix, int rows, int cols) {
        int m = matrix.length; int n = matrix[0].length;
        if (m * n != rows * cols) {
            return matrix;
        }
        int[][] res = new int[rows][cols];
        int index = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.println(index / n + " " + (index % n) + " ");
                res[i][j] = matrix[index / n][index % n];
                index++;
            }
        }
        return res;
    }
}
